package com.homedo.microservice.open.service.gateway.manager.web.remote.api.impl;

import com.homedo.common.bean.data.RequestData;
import com.homedo.common.bean.data.ResponseData;
import com.homedo.microservice.open.service.gateway.manager.web.api.IDbSourceServiceRemote;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.DbSourceAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.DbSourceUpdateAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.DbSourceAResp;
import com.homedo.microservice.open.service.gateway.manager.web.bean.po.DbSource;
import com.homedo.microservice.open.service.gateway.manager.web.bean.po.enhanced.DbSourceEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.convent.CommonConvent;
import com.homedo.microservice.open.service.gateway.manager.web.service.DbSourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DbSourceServiceRemoteImpl implements IDbSourceServiceRemote {
    @Autowired
    private DbSourceService dbSourceService;

    @Override
    public ResponseData<DbSourceAResp> detail(@RequestBody RequestData<DbSourceAReq> requestData) {
        ResponseData<DbSourceAResp> responseData = new ResponseData<>();

        DbSourceAReq dbSourceAReq = requestData.getRequest();

        DbSourceEnhanced dbSourceEnhanced = dbSourceService.getEnhanceds(dbSourceAReq.getServiceGatewayName(), dbSourceAReq.getEnv(), null);

        if (dbSourceEnhanced != null) {
            responseData.setData(CommonConvent.convent2DbSourceAResp(dbSourceEnhanced));
        }

        return responseData;
    }

    @Override
    public ResponseData update(@RequestBody RequestData<DbSourceUpdateAReq> requestData) {
        ResponseData responseData = new ResponseData();

        DbSource dbSource = new DbSource();
        BeanUtils.copyProperties(requestData.getRequest(), dbSource);

        dbSourceService.updateDbSource(dbSource, null);

        return responseData;
    }
}
